// pages/singerSearch/singerSearch.js
var app = getApp();
var util = require("../../utils/util");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hotData: [],
    // name: "",
    // singerid: "",
    // imgurl: "",
    hotShow: true, //热门搜索是否显示，默认进来显示
    searchValue: "", //input实时搜索内容
    searchSongsPageTotal: 0, //搜索歌曲总页数
    searchResSongsList: [], //歌曲搜索结果
    ResultSearchShow: false, //用户是否确认搜索或者点击模糊查询的结果
    nowSearchShow: false, //实时搜索是否展示
    dataCut: false, //是否对数据进行去重处理
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
  },
  // 搜索框输入时
  input: function (e) {
    // console.log(e.detail.value);
    var that = this;
    that.setData({
      hotShow: false,
      nowSearchShow: true,
      searchValue: e.detail.value,
    });
    that.searchConfirm(1, 1);
  },
  //点击完成按钮时触发
  inputConfirm: function (e) {
    console.log(e.detail.value, "6666");
    var that = this;
    that.setData({
      hotShow: false,
      nowSearchShow: false,
      searchValue: e.detail.value,
      ResultSearchShow: true,
    });
    that.searchConfirm(1, 0);
  },
  // 搜索结果
  // operationType:是否对搜索的结果进行去重处理   1:去重处理；0：不处理
  searchConfirm: function (searchPageNum, operationType) {
    var that = this;
    console.log(that.data.searchValue, "搜索时获取到的");
    let params = {
      filter: [
        {
          matchType: "like",
          matchName: "name",
          matchValue: that.data.searchValue,
        },
      ],
      per_page: 10,
      page: searchPageNum,
    };
    wx.request({
      url: util.api + "/singers",
      data: JSON.stringify(params),
      method: "POST",
      header: {
        "Content-Type": "application/json",
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.status_code==200) {
          that.setData({
            searchResSongsList:res.data.data.data,
            searchSongsPageTotal:res.data.data.total,
          })
        }
      },
    });
    // util.postRes("singers", JSON.stringify(params), function (res) {
    //   console.log(res, "1111");
    //   that.setData({
    //     searchSongsPageTotal: res.data.singers.total,
    //     searchResSongsList: res.data.singers.data,
    //   });
    // });
  },
  // 实时搜索结果的点击事件
  onHotkeyTap: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.name, "9");
    that.setData({
      hotShow: false,
      nowSearchShow: false,
      ResultSearchShow: true,
      searchValue: e.currentTarget.dataset.name,
    });
    that.searchConfirm(1, 0);
  },
  //热门搜索的点击事件
  hotClick: function (e) {
    // {
    //   "filter":[{"matchType":"like","matchName":"singer","matchValue":"周杰伦"}],
    //   "per_page":10,
    //   "page":1
    //   }
    var that = this;
    console.log(e.currentTarget.dataset.name, "8");
    that.setData({
      hotShow: false,
      nowSearchShow: false,
      ResultSearchShow: true,
      searchValue: e.currentTarget.dataset.name,
    });
    that.searchConfirm(1, 0);
  },
  // 点击跳转歌手主页
  bindNavView: function (e) {
    console.log(e.currentTarget.dataset);
    wx.navigateTo({
      url:
        "../singerIndex/singerIndex?id=" +
        e.currentTarget.dataset.id +
        "&name=" +
        e.currentTarget.dataset.name +
        "&imgurl=" +
        e.currentTarget.dataset.imgurl,
    });
  },
  // 点击取消和图标
  cancelSearch: function (e) {
    var that = this;
    that.setData({
      searchValue: "",
      hotShow: true,
      nowSearchShow: false,
      ResultSearchShow: false,
      searchResSongsList: [],
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    let data = {
      per_page: 10,
      page: 1,
    };
    var dataStr = JSON.stringify(data);
    wx.request({
      url: util.api+"/singers?category=1",
      data: dataStr,
      method: "POST",
      success(res) {
        if (res.data.status_code == 200) {
          that.setData({
            hotData: res.data.data.data.splice(0, 6),
          });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
